package org.dxw.common.excel;

import cn.hutool.core.date.DateTime;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.CellData;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

import java.math.BigDecimal;

/**
 * @Author dxw
 * @Date 2022/12/31 13:55
 * @Description
 */
public class EasyExcelConvert {

    public static abstract class SupportConvert<T> implements Converter<T>{

        @Override
        public Class<?> supportJavaTypeKey() {
            return Converter.super.supportJavaTypeKey();
        }

        @Override
        public CellDataTypeEnum supportExcelTypeKey() {
            return Converter.super.supportExcelTypeKey();
        }

        @Override
        public T convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
            return Converter.super.convertToJavaData(cellData, contentProperty, globalConfiguration);
        }

        @Override
        public T convertToJavaData(ReadConverterContext<?> context) throws Exception {
            return Converter.super.convertToJavaData(context);
        }

        @Override
        public WriteCellData<?> convertToExcelData(T value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
            return Converter.super.convertToExcelData(value, contentProperty, globalConfiguration);
        }

        @Override
        public WriteCellData<?> convertToExcelData(WriteConverterContext<T> context) throws Exception {
            return Converter.super.convertToExcelData(context);
        }
    }


    public static class BigDecimalScale2Convert extends SupportConvert<BigDecimal>{

        @Override
        public WriteCellData<BigDecimal> convertToExcelData(BigDecimal value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
            if(value != null){
                value = value.setScale(2, BigDecimal.ROUND_HALF_UP);
            }
            return new WriteCellData(value);
        }

    }


}
